package com.hamll.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamll.common.dto.ItemDTO;
import com.hamll.common.dto.PageDTO;
import com.hamll.item.pojo.Item;
import com.hamll.item.service.IItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    //分页
    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam("page") Integer page,
                              @RequestParam("size") Integer size) {

        return itemService.pageItem(page, size);
    }


    //远程调用用的
    @GetMapping("/findByPage")
    public PageDTO<ItemDTO> findByPage4Search(
            @RequestParam(name = "page") Integer page,
            @RequestParam(name = "size") Integer size) {
        //分页条件
        Page<Item> ipage = new Page<>(page, size);
        //查询条件
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper();
        // 只查询 正常数据
        queryWrapper.eq(Item::getStatus, 1);
        // 根据id 正序
        queryWrapper.orderByAsc(Item::getId);

        Page<Item> itemPage = itemService.page(ipage, queryWrapper);

        List<Item> itemList = itemPage.getRecords();

//        对象转换
        List<ItemDTO> itemDTOList = new ArrayList<>();
        for (Item item : itemList) {
            ItemDTO itemDTO = new ItemDTO();
            BeanUtils.copyProperties(item, itemDTO);
            itemDTOList.add(itemDTO);
        }

        return new PageDTO<ItemDTO>(itemPage.getTotal(), itemDTOList);
    }

    //根据id查询商品
    @GetMapping("/{id}")
    public Item queryItemById(@PathVariable(name = "id") Long id) {
        return itemService.queryItemById(id);
    }

    @GetMapping("/getById/{id}")
    public ItemDTO getById(@PathVariable(name = "id")Long id){
        Item item = itemService.getById(id);
        ItemDTO itemDTO = new ItemDTO();
        BeanUtils.copyProperties(item,itemDTO);
        return itemDTO;
    }

    //添加商品
    @PostMapping
    public void addItem(@RequestBody Item item) {

        itemService.addItem(item);

    }

    //商品上下架
    @PutMapping("/status/{id}/{status}")
    public void upOrDownItem(@PathVariable(name = "id") Long id,
                             @PathVariable(name = "status") Integer status) {

        itemService.upOrDownItem(id, status);
    }

    //修改商品
    @PutMapping
    public void updateItem(@RequestBody Item item) {
        itemService.updateItem(item);

    }


    @DeleteMapping("/{id}")
    public void deleteItemById(@PathVariable(name = "id") Long id) {

        itemService.deleteItemById(id);
    }

    /**
     * 修改库存
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable(name = "itemId") Long itemId,
                            @PathVariable(name = "num") Integer num){
        itemService.deductStock(itemId, num);
    }
}
